import java.util.LinkedList;
import java.util.Queue;

/**
 * @author LKQ
 * @date 2022/1/29 10:08
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] isWater = {{0,0,1}, {1,0,0}, {0, 0, 0}};
        solution.highestPeak(isWater);
    }
    public int[][] highestPeak(int[][] isWater) {
        int m = isWater.length, n = isWater[0].length;
        // 初始所有陆地和水域都未访问
        boolean[][] visit = new boolean[m][n];
        // 存储初始水域位置
        Queue<int[]> queue = new LinkedList<>();
        int[][] height = new int[m][n];
        // 东南西北四个方向
        int[][] directions = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
        // 初始化水域位置
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (isWater[i][j] == 1) {
                    queue.add(new int[]{i, j});
                }
            }
        }
        while (!queue.isEmpty()) {
            int[] temp = queue.poll();
            int x = temp[0], y = temp[1];
            for (int i = 0; i < 4; i++) {
                int nX = x + directions[i][0], nY = y + directions[i][1];
                if (nX >= 0 && nX < m && nY >= 0 && nY < n && !visit[nX][nY] && isWater[nX][nY] != 1) {
                    height[nX][nY] = height[x][y] + 1;
                    visit[nX][nY] = true;
                    queue.offer(new int[] {nX, nY});
                }
            }
        }
        return height;
    }
}
